package com.ruoyi.project.blog.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.ResponseResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Anonymous;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.blog.domain.BArticle;
import com.ruoyi.project.blog.domain.dto.ArticleDTO;
import com.ruoyi.project.blog.domain.vo.ApiArticleListVO;
import com.ruoyi.project.blog.domain.vo.ApiCommentListVO;
import com.ruoyi.project.blog.domain.vo.SystemArticleListVO;
import com.ruoyi.project.blog.service.IBArticleService;
import com.ruoyi.project.blog.service.IBCommentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 博客文章Controller
 *
 * @author ruoyi
 * @date 2023-09-21
 */
@RestController
@RequestMapping("/articles/article")
public class BArticleController extends BaseController {
    @Autowired
    private IBArticleService bArticleService;
    @Autowired
    private IBCommentService ibCommentService;

    @GetMapping(value = "/")
    @Anonymous
    public TableDataInfo selectArticleList(Long categoryId, Long tagId, String orderByDescColumn, Long userId) {
        startPage();
        List<ApiArticleListVO> list = bArticleService.selectArticleList(categoryId, tagId, orderByDescColumn, userId);
        return getDataTable(list);
    }

    @Anonymous
    @GetMapping(value = "/selectUserInfoByArticleId")
    @ApiOperation(value = "根据文章id获取作者信息", httpMethod = "GET", response = ResponseResult.class, notes = "根据文章id获取作者信息")
    public ResponseResult selectUserInfoByArticleId(Long id) {
        return bArticleService.selectUserInfoByArticleId(id);
    }

    @GetMapping(value = "/like")
    @ApiOperation(value = "文章点赞", httpMethod = "GET", response = ResponseResult.class, notes = "文章点赞")
    @Anonymous
    public ResponseResult articleLike(Long articleId) {
        return bArticleService.articleLike(articleId);
    }

    @Anonymous
    @RequestMapping(value = "/comments", method = RequestMethod.GET)
    public TableDataInfo selectCommentByArticleId(Long articleId) {
        startPage();
        List<ApiCommentListVO> listVOS = ibCommentService.selectCommentByArticleId(articleId);
        return getDataTable(listVOS);
    }

    @GetMapping(value = "/info")
    @Anonymous
    public ResponseResult selectArticleInfo(Long id) {
        return bArticleService.selectArticleInfo(id);
    }

    @Anonymous
    @GetMapping(value = "/archive")
    @ApiOperation(value = "归档", httpMethod = "GET", response = ResponseResult.class, notes = "归档")
    public ResponseResult archive(Long userId) {
        return bArticleService.archive(userId);
    }

    @Anonymous
    @GetMapping(value = "/search")
    @ApiOperation(value = "用户搜索文章", httpMethod = "GET", response = ResponseResult.class, notes = "用户搜索文章")
    public TableDataInfo searchArticle(String keyword) {
        startPage();
        List<ApiArticleListVO> list = bArticleService.searchArticle(keyword);
        return getDataTable(list);
    }

    /**
     * 查询博客文章列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BArticle bArticle) {
        //判断是否是管理员，管理员查看所有数据，否则值查看自己的数据
        boolean admin = SecurityUtils.isAdmin(SecurityUtils.getUserId());
        if (!admin) {
            bArticle.setUserId(SecurityUtils.getUserId().toString());
        }
        startPage();
        List<SystemArticleListVO> list = bArticleService.selectBArticleList(bArticle);
        return getDataTable(list);
    }

    /**
     * 导出博客文章列表
     */
    @Log(title = "文章管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BArticle bArticle) {
        //判断是否是管理员，管理员查看所有数据，否则值查看自己的数据
        boolean admin = SecurityUtils.isAdmin(SecurityUtils.getUserId());
        if (!admin) {
            bArticle.setUserId(SecurityUtils.getUserId().toString());
        }
        List<SystemArticleListVO> list = bArticleService.selectBArticleList(bArticle);
        ExcelUtil<SystemArticleListVO> util = new ExcelUtil<SystemArticleListVO>(SystemArticleListVO.class);
        util.exportExcel(response, list, "博客文章数据");
    }

    /**
     * 获取博客文章详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bArticleService.selectArticleById(id));
    }

    /**
     * 新增博客文章
     */
    @Log(title = "文章管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArticleDTO bArticle) {
        bArticle.setUserId(SecurityUtils.getUserId().toString());
        return bArticleService.insertBArticle(bArticle);
    }

    /**
     * 修改博客文章
     */
    @Log(title = "文章管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BArticle bArticle) {
        return toAjax(bArticleService.updateBArticle(bArticle));
    }

    /**
     * 删除博客文章
     */
    @Log(title = "文章管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return bArticleService.deleteBArticleByIds(ids);
    }

    @GetMapping(value = "/randomImg")
    @Log(title = "文章管理", businessType = BusinessType.OTHER)
    public AjaxResult randomImg() {
        return bArticleService.randomImg();
    }

    @PostMapping(value = "/pubOrShelf")
    @Log(title = "文章管理", businessType = BusinessType.UPDATE)
    public AjaxResult psArticle(@RequestBody ArticleDTO article) {
        return bArticleService.psArticle(article);
    }

}
